
import json
import requests

# Load the JSON data
with open('/Users/bingma/py/zihu_scrapy/zhihu/example.json', 'r', encoding='utf-8') as file:
    data = json.load(file)

# Extract the highest quality video URL
video_playlist = data['appContext']['__connectedAutoFetch']['trainingVideo']['data']['videoInfo']['resource']['data']['playlist']
highest_quality = max(video_playlist.keys(), key=lambda k: video_playlist[k]['height'])
video_url = video_playlist[highest_quality]['url']

# Download the highest quality video
video_response = requests.get(video_url)
with open('highest_quality_video.mp4', 'wb') as video_file:
    video_file.write(video_response.content)

# Extract and download PDF files
file_list = data['appContext']['__connectedAutoFetch']['trainingVideo']['data']['videoInfo']['fileList']
for file_info in file_list:
    file_id = file_info['fileId']
    pdf_url = f'https://api.zhihu.com/education/file/{file_id}'
    pdf_response = requests.get(pdf_url)
    with open(file_info['fileName'], 'wb') as pdf_file:
        pdf_file.write(pdf_response.content)